package org.lee.selenium.app;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;
import java.util.List;

/**
 * App
 *
 * @author dralee
 * @date 2021-11-26 16:27:09
 **/
public class App {

    private static WebDriver initDriver(DriverType driverType) {
        String path = "D:\\Document\\LeeSp\\javaspace\\selenium-demo\\driver\\";
        switch (driverType) {
            case CHROME:
                System.setProperty("webdriver.chrome.driver", path.concat("chromedriver.exe"));
                return new ChromeDriver();
            case EDGE:
            default:
                System.setProperty("webdriver.edge.driver", path.concat("chromedriver.exe"));
                return new EdgeDriver();
            case FIREFOX:
                System.setProperty("webdriver.gecko.driver", path.concat("geckodriver.exe"));
                return new FirefoxDriver();
            case IE:
                System.setProperty("webdriver.ie.driver", path.concat("IEDriverServer.exe"));
                return new InternetExplorerDriver();
        }
    }

    public static void main1(String[] args) {

        WebDriver driver = initDriver(DriverType.CHROME);
//        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        try {
            driver.manage().timeouts().implicitlyWait(Duration.ofMinutes(5));
            driver.get("https://www.baidu.com/");
            WebElement login = driver.findElement(By.cssSelector("#s-top-left a"));
            login.click();
            List<WebElement> elements = driver.findElements(By.className("showcase__item"));
            for (WebElement element : elements) {
                element.click();
                WebElement btnDownload = driver.findElement(By.className("download-button"));
                if (btnDownload != null) {
                    btnDownload.click();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            driver.quit();
        }
    }

    public static void main(String[] args) {

        WebDriver driver = initDriver(DriverType.CHROME);
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        try {
            driver.manage().timeouts().implicitlyWait(Duration.ofMinutes(5));
            driver.get("https://www.freepik.com/search?dates=any&format=search&freepik_choice=1&page=1&selection=1&sort=popular");
            WebElement login = driver.findElement(By.cssSelector("[data-tab=tab-login]"));
            login.click();
            List<WebElement> elements = driver.findElements(By.className("showcase__item"));
            for (WebElement element : elements) {
                element.click();
                WebElement btnDownload = driver.findElement(By.className("download-button"));
                if (btnDownload != null) {
                    btnDownload.click();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            driver.quit();
        }
    }
}
